/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fullalbum.main;

import fullalbum.dao.FigureDAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author leoomoreira
 */
public class QueryUserRepeatedFigureSuggestedServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("application/xml;charset=ISO-8859-1");
        String xml = "";

        String userId = request.getParameter("userId");
        String albumId = request.getParameter("albumId");

        HttpSession session = request.getSession();

        FigureDAO figureDAO = new FigureDAO();

        xml += "<result>";
        if (true) {
            xml += "<status>success</status>";
            List<String> repeatedFigure = figureDAO.getRepeatedFiguresIdSuggested(Integer.parseInt(session.getAttribute("userId").toString()), Integer.parseInt(userId), Integer.parseInt(albumId));
            xml += "<repeatedfigure>";
            for (int i = 0; i < repeatedFigure.size(); i++) {
                xml += repeatedFigure.get(i);
                if (i != repeatedFigure.size() - 1)
                    xml += ", ";
            }
            xml += "</repeatedfigure>";
            List<String> exchangeFigure = figureDAO.getExchangeFiguresIdSuggested(Integer.parseInt(session.getAttribute("userId").toString()), Integer.parseInt(userId), Integer.parseInt(albumId));
            xml += "<exchangefigure>";
            for (int i = 0; i < exchangeFigure.size(); i++) {
                xml += exchangeFigure.get(i);
                if (i != exchangeFigure.size() - 1)
                    xml += ", ";
            }
            xml += "</exchangefigure>";
        }
        else {
            xml += "<status>failure</status>";
        }
        xml += "</result>";
        PrintWriter out = response.getWriter();
        try {
            out.println(xml);
        } finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
